package com.bbm.communitysystem.controller;


import cn.hutool.core.bean.BeanUtil;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.Community;
import com.bbm.communitysystem.domain.Household;
import com.bbm.communitysystem.domain.bo.HouseholdBo;
import com.bbm.communitysystem.domain.dto.HouseholdDto;
import com.bbm.communitysystem.domain.vo.HouseholdVo;
import com.bbm.communitysystem.service.IHouseholdService;
import com.bbm.communitysystem.service.impl.CommunityServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-02-21
 */
@Tag(name = "住户")
@RestController
@RequestMapping("/household")
@RequiredArgsConstructor
public class HouseholdController {

    private final IHouseholdService householdService;

    private final CommunityServiceImpl communityService;

    @Operation(summary = "创建住户")
    @PostMapping("")
    public R createHousehold(@RequestBody HouseholdDto householdDto) {

        HouseholdBo householdBo = BeanUtil.copyProperties(householdDto, HouseholdBo.class);

        int household = householdService.createHousehold(householdBo);

        return household == 1 ? R.ok("创建成功") : R.error("创建失败");
    }

    //根据家庭id获取住户
    @Operation(summary = "根据家庭id获取住户")
    @GetMapping("/{familyId}")
    public R<List<HouseholdVo>> getHouseholdByFamilyId(@PathVariable("familyId") Integer familyId) {

        List<Household> list = householdService.lambdaQuery()
                .eq(Household::getFamilyId, familyId)
                .list();

        List<HouseholdVo> list1 = new ArrayList<>();

        list.forEach(item -> {
            HouseholdVo householdVo = getHouseholdVo(item);
            list1.add(householdVo);
        });

        return R.ok("获取成功", list1);
    }

    //根据userId获取住户
    @Operation(summary = "根据userId获取住户")
    @GetMapping("/user/{userId}")
    public R<HouseholdVo> getHouseholdByUserId(@PathVariable("userId") Integer userId) {
        Household item = householdService.lambdaQuery()
                .eq(Household::getUserId, userId)
                .one();

        HouseholdVo householdVo = getHouseholdVo(item);

        return R.ok("获取成功", householdVo);
    }

    @Operation(summary = "根据住户id获取住户")
    @GetMapping("/household/{id}")
    public R<HouseholdVo> getHouseholdById(@PathVariable("id") Integer id) {
        Household byId = householdService.getById(id);

        return byId != null ? R.ok(getHouseholdVo(byId)) : R.error("获取失败");
    }

    @Operation(summary = "更新住户信息")
    @PutMapping
    public R updateHousehold(@RequestBody HouseholdDto dto) {
        int result = householdService.updateHousehold(dto);
        return result > 0 ? R.ok("更新成功") : R.error("更新失败");
    }

    @Operation(summary = "删除住户")
    @DeleteMapping("/{id}")
    public R deleteHousehold(@PathVariable Integer id) {
        return householdService.removeById(id) ?
                R.ok("删除成功") : R.error("删除失败");
    }

    private HouseholdVo getHouseholdVo(Household item) {
        HouseholdVo householdVo = new HouseholdVo();
        householdVo.setId(item.getId());
        householdVo.setHouseholdName(item.getHouseholdName());
        householdVo.setHouseholdRmake(item.getHouseholdRmake());
        householdVo.setPhone(item.getPhone());
        householdVo.setUserId(item.getUserId());
        householdVo.setCardNum(item.getCardNum());

        if (item.getSex() == null) {
            householdVo.setSex("未设置");
        } else {
            householdVo.setSex(item.getSex() ? "男" : "女");
        }


        householdVo.setBirthDate(item.getBirthDate());
        householdVo.setCurReside(item.getCurReside());
        householdVo.setEthnicGroup(item.getEthnicGroup());
        householdVo.setCommunilyId(item.getCommunilyId());
        householdVo.setFamilyId(item.getFamilyId());
        Community byId = communityService.getById(item.getCommunilyId());

        if (byId == null) {
            householdVo.setCommunilyName("无");
        } else {
            householdVo.setCommunilyName(byId.getCommunityName());
        }


        householdVo.setUpdateBy(item.getUpdateBy());
        householdVo.setUpdateTime(item.getUpdateTime());

        householdVo.setEthnicGroup(item.getEthnicGroup());
        return householdVo;
    }

}
